package AdamsDevice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;

import javax.swing.JTextArea;

/**
 * A simple class that opens a socket, sends a message to the server, and
 * terminates.
 * @author Graeme Stevenson (graeme.stevenson@ucd.ie)
 */
public class TCPSocketClientremote {

	/**
	 * The server host name.
	 */
	public String my_serverHost;

	/**
	 * The server port.
	 */
	public int my_serverPort;
	
	public boolean play_mode = false;

	/**
	 * Sets the client up with the server details.
	 * @param the_serverHost the server host name.
	 * @param the_serverPort the server port.
	 */

	public Vector<String> movieList;

	public JTextArea consoleWindow;

	public TCPSocketClientremote(String the_serverHost, int the_serverPort,  JTextArea the_console) {
		my_serverHost = the_serverHost;
		my_serverPort = the_serverPort;
		consoleWindow = the_console;
	}

	/**
	 * Creates a connection to the server and sends a message.
	 * @param a_message the message to send to the server.
	 */
	public void sendMessage(String a){
		try {
			// Create a connection to the server.
			Socket toServer = new Socket(my_serverHost, my_serverPort);
			ListParser LP = new ListParser();

			PrintWriter out = new PrintWriter(toServer.getOutputStream(), true);

			// Write the message to the socket.
			out.println(a);

			BufferedReader in = new BufferedReader(new InputStreamReader(toServer
					.getInputStream()));

			if(a.equalsIgnoreCase("list")){
				movieList = new Vector<String>();
				String msg = in.readLine();
				consoleWindow.append("\n" + msg + "\n");
				movieList = LP.MakeList(msg);
				System.out.println(movieList);

			}else if(a.split("->")[0].equalsIgnoreCase("play")){
				String msg = in.readLine();
				if(msg.split("->")[0].equalsIgnoreCase("playing")){
					consoleWindow.append(msg + "\n");
					play_mode = true;
				}
			}else{
				String msg = in.readLine();
				consoleWindow.append(msg + "\n");
			}

			//out.close();
			//toServer.close();

		} catch (IOException ioe) {
			ioe.printStackTrace();
		} catch (SecurityException se) {
			se.printStackTrace();
		}
	}

} // end class

